package com.wentao.thisService.controller;

import com.wentao.thisCommon.annotation.MethodAuthority;
import com.wentao.thisCommon.model.RequestBase;
import com.wentao.thisCommon.model.ResultBase;
import com.wentao.thisFacade.business.AuthorityCtrInterface;
import com.wentao.thisFacade.common.UserInfo;
import com.wentao.thisFacade.common.enums.ExceptionCode;
import com.wentao.thisFacade.common.enums.SuccessCode;
import com.wentao.thisFacade.domain.reqeust.ForgetPasswordRequest;
import com.wentao.thisFacade.domain.reqeust.LoginRequest;
import com.wentao.thisFacade.domain.reqeust.MobileAndCodeRequest;
import com.wentao.thisFacade.domain.reqeust.MobileCodeRequest;
import com.wentao.thisService.service.AuthenticationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class AuthorityController implements AuthorityCtrInterface {

    @Autowired
    private AuthenticationService authenticationService;

    /**
     * @see AuthorityCtrInterface#login(RequestBase)
     */
    @Override
//    @MethodAuthority
    public ResultBase<UserInfo> login(@RequestBody RequestBase<LoginRequest> entity) {
        try {
            UserInfo userInfo = authenticationService.login(entity.getBody().getUserName(), entity.getBody().getPassword(), null);
            return ResultBase.ok(userInfo, SuccessCode.SYSTEM_SUCCESS);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultBase.ok(ExceptionCode.ON_FAILURE);
        }
    }

    /**
     * @see AuthorityCtrInterface#mobileCodeGain(RequestBase)
     */
    @Override
    public ResultBase<Boolean> mobileCodeGain(RequestBase<MobileCodeRequest> entity) {
        try {
            Boolean gainFlag = authenticationService.mobileCodeGain(entity.getBody());
            return ResultBase.ok(gainFlag, SuccessCode.SYSTEM_SUCCESS);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultBase.ok(ExceptionCode.ON_FAILURE);
        }
    }

    /**
     * @see AuthorityCtrInterface#mobileAndCodeLogin(RequestBase)
     */
    @Override
    public ResultBase<UserInfo> mobileAndCodeLogin(RequestBase<MobileAndCodeRequest> entity) {
        try {
            UserInfo userInfo = authenticationService.mobileAndCodeLogin(entity.getBody());
            return ResultBase.ok(userInfo, SuccessCode.SYSTEM_SUCCESS);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultBase.ok(ExceptionCode.ON_FAILURE);
        }
    }

    /**
     * @see AuthorityCtrInterface#loginByUserNameAndToken(RequestBase)
     */
    @Override
//    @MethodAuthority
    public ResultBase<UserInfo> loginByUserNameAndToken(RequestBase<UserInfo> entity) {
        try {
            UserInfo userInfo = authenticationService.loginByUserNameAndToken(entity.getBody().getUserName(), entity.getBody().getToken());
            return ResultBase.ok(userInfo, SuccessCode.SYSTEM_SUCCESS);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultBase.ok(ExceptionCode.ON_FAILURE);
        }
    }

    /**
     * @see AuthorityCtrInterface#changePassword(RequestBase)
     */
    @Override
//    @MethodAuthority
    public ResultBase<Boolean> changePassword(RequestBase<ForgetPasswordRequest> entity) {
        try {
            Boolean changePasswordFlag = authenticationService.changePassword(entity.getBody());
            return ResultBase.ok(changePasswordFlag, SuccessCode.SYSTEM_SUCCESS);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultBase.ok(ExceptionCode.ON_FAILURE);
        }
    }


    /**
     * @param entity
     * @return
     * @desc 登出
     * @author huabin
     */
    @Override
//    @MethodAuthority
    public ResultBase<Boolean> loginOut(@RequestBody RequestBase<LoginRequest> entity) {
        try {
            Boolean loginOutFlag = authenticationService.loginOut(entity.getBody().getUserName());
            return ResultBase.ok(loginOutFlag, SuccessCode.SYSTEM_SUCCESS);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultBase.ok(ExceptionCode.ON_FAILURE);
        }
    }
}
